package com.lucher.mes.quartz;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob;

import com.lucher.mes.common.HttpRequest;
import com.lucher.mes.common.Tools;
import com.lucher.mes.common.Uuid;
import com.lucher.mes.controller.SpringUtils;
import com.lucher.mes.dao.PrisonerMapper;
import com.lucher.mes.dao.RollcallMapper;
import com.lucher.mes.dao.RollcallinfoMapper;
import com.lucher.mes.po.Prisoner;
import com.lucher.mes.po.Rollcall;
import com.lucher.mes.po.Rollcallinfo;
import com.lucher.mes.service.ISystemparaService;

public class RollcallJob extends StatefulMethodInvokingJob {
	
    private static final Logger logger = Logger.getLogger(RollcallJob.class);
 
    /**
     * @see org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.MethodInvokingJob#executeInternal(org.quartz.JobExecutionContext)
     */
 
    @Override
    protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
        logger.info("================== AutoRollcall job start =================");
        
        //普通类从spring容器中拿出service
      	RollcallMapper rollcallDao = (RollcallMapper)SpringUtils.getBean("rollcallMapper");
      	RollcallinfoMapper rollcallinfoDao = (RollcallinfoMapper)SpringUtils.getBean("rollcallinfoMapper");
      	PrisonerMapper prisonerDao = (PrisonerMapper)SpringUtils.getBean("prisonerMapper");
      	ISystemparaService systemparaService = (ISystemparaService)SpringUtils.getBean("systemparaService");
        
        //保存材料信息
        String uuid = Uuid.generateShortUuid();
  		String cuurenttime = Tools.getCurrentTime();
        
        Rollcall rollcall = new Rollcall();
        rollcall.setUuid(uuid);
        rollcall.setAddtime(cuurenttime);
  		Integer rollcallId = rollcallDao.insertSelective(rollcall);
  		
  		//批量插入所有人员的签到准备信息
  		ArrayList<Rollcallinfo> rollcallinfolist = new ArrayList<Rollcallinfo>();
  		//查询所有的人员
  		Prisoner record = new Prisoner();
  		//record.setStatus("入狱");
  		List<Prisoner> prisonerlist = prisonerDao.findPrisonerList(record);
  		for (Prisoner prisoner: prisonerlist) {
  			Rollcallinfo rollcallinfo = new Rollcallinfo();
  			rollcallinfo.setUuid(uuid);
  			rollcallinfo.setPrisonercode(prisoner.getPrisonercode());
  			rollcallinfo.setPrisonername(prisoner.getPrisonername());
  			rollcallinfo.setStatus("未签到");
  			rollcallinfolist.add(rollcallinfo);
  		}
  		if(rollcallinfolist != null && rollcallinfolist.size() > 0){
  			rollcallinfoDao.saveBatch(rollcallinfolist);
  		}
  		
  		//发送点名指令
  		String send_rollcall_url = systemparaService.findSystemParaByCodeStr("send_rollcall_url");
  		String sr= HttpRequest.sendPost(send_rollcall_url, "uuid=" + uuid);
        
        logger.info("==================  AutoRollcall job end =================");
    }
 
}
